<#include '/admin/header.html' >

<link rel="stylesheet"  href="${ctx}/adminthemes/version3/css/demo.css" />
<link rel="stylesheet"  href="${ctx}/adminthemes/version3/css/zTreeStyle/zTreeStyle.css"/>

<SCRIPT type="text/javascript">
		var isSave=1;
		var parent_id=1;
		var setting = {
			edit: {
				enable: true,
				showRemoveBtn: false,
				showRenameBtn: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			async: {
	            enable: true,
	            url:"${ctx}/cms/admin/cat/list-json-help.do",
	            autoParam:["cat_id"]
	        },
			callback: {
				beforeDrag: beforeDrag,
				beforeDrop: beforeDrop,
				onAsyncSuccess: zTreeOnAsyncSuccess,
				onClick: zTreeOnClick,
				onRename: zTreeOnRename,
				beforeRemove: beforeRemove,
				onRemove: onRemove
				
			}
		};
		
		
		
		//初始化完成tree后默认选中第一个选项
		function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = treeObj.getNodes();
			if (nodes.length>0){
				treeObj.selectNode(nodes[0]);
				$("#name").val(nodes[0].name);
				$("#url").val(nodes[0].url);
				
				if(nodes[0].if_audit == 1){
						$(".is_default_yes").attr("checked","checked");
				}else{
						$(".is_default_no").attr("checked","checked");
				}
				$("#cat_order").val(nodes[0].cat_order);
				$(".view").html(123);
				
				
				
			}
		};
		
		var newCount = 1;
		function add(e) {
			if(isSave == 0){
				parent.$.Loading.error("请先保存当前节点信息后再操作!");
				return;
			}
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			isParent = e.data.isParent,
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			parent_id=treeNode.cat_id;
			if (treeNode) {
				treeNode = zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, isParent:isParent, name:"node" + (newCount++)});
			} else {
				treeNode = zTree.addNodes(null, {id:(100 + newCount), pId:0, isParent:isParent, name:"node" + (newCount++)});
			}
			if (treeNode) {
				zTree.editName(treeNode[0]);
			} else {
				alert("叶子节点被锁定，无法增加子节点");
			}
		};
		//重命名方法
		function zTreeOnRename(event, treeId, treeNode, isCancel) {
			$("#name").val(treeNode.name);
			$("#url").val(treeNode.url);
			$("#cat_order").val(1);
			isSave=0;
		}
		
		//删除节点
		function beforeRemove(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
			return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
		}
		function onRemove(e, treeId, treeNode) {
			showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		}
		function remove(e) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			if (nodes.length == 0) {
				alert("请先选择一个节点");
				return;
			}
			var callbackFlag = $("#callbackTrigger").attr("checked");
			zTree.removeNode(treeNode, callbackFlag);
		};
		//点击tree选项事件
		function zTreeOnClick(event, treeId, treeNode) {
			$("#name").val(treeNode.name);
			$("#url").val(treeNode.url);
			$("#cat_order").val(treeNode.cat_order);
			$("#editor1").val(123);
			
		};
		
		function beforeDrag(treeId, treeNodes) {
			for (var i=0,l=treeNodes.length; i<l; i++) {
				if (treeNodes[i].drag === false) {
					return false;
				}
			}
			return true;
		}
		function beforeDrop(treeId, treeNodes, targetNode, moveType) {
			return targetNode ? targetNode.drop !== false : true;
		}
		
		function showCode(id, str) {
			var code = $("#code" + id);
			code.empty();
			for (var i=0, l=str.length; i<l; i++) {
				code.append("<li>"+str[i]+"</li>");
			}
		}
	
		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting);
			$("#addParent").bind("click", {isParent:true}, add);
			$("#addLeaf").bind("click", {isParent:false}, add);
			$("#remove").bind("click", remove);
		});
	</SCRIPT>
</HEAD>

<BODY>
<div class="content_wrap" style="width:100%;">
	<div class="zTreeDemoBackground left" style="float:left;margin: 0 0 0 10px;">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
	
	<div class="" style="float:left;width:400px;">
		<form id="editForm" class="layui-form layui-form-pane" action="" enctype="multipart/form-data" style="padding-top:10px">
  		<div class="layui-form-item">
    		<label class="layui-form-label">分类名称：</label>
    		<div class="layui-input-block">
      			<input type="text" id="name" name="name" maxlength="20" value="" lay-verify="name" autocomplete="off" class="layui-input">
    		</div>
  		</div>
  		<div class="layui-form-item">
    		<label class="layui-form-label">文章模型：</label>
    		<div class="layui-input-block">
      			<select id="selector" name="model_id">
      				 <#list modelList as model>
						<option value="${model.model_id }" >${model.china_name }</option>
					</#list>	
      			</select>
    		</div>
  		</div>
  		 <div class="layui-form-item">
    		<label class="layui-form-label">是否加入SiteMap：</label>
    		<div class="layui-input-block aa" >
				<input name="if_audit " value="1"  title="是"  type="radio" class="is_default_yes">
			    <input name="if_audit" value="0"  title="否"  type="radio" class="is_default_no"> 
    		</div>
  		</div>
  		 
  		<div class="layui-form-item">
    		<label class="layui-form-label">页面地址：</label>
    		<div class="layui-input-block">
      			<input type="text" id="url" name="url" maxlength="30"  class="layui-input" style="display:inline;">&nbsp;
    		</div>
  		</div>
  		<div class="layui-form-item" style="width: 100%">
			<label class="layui-form-label">分类描述：</label>
			<div class="layui-input-block">
				<script id="editor1" name="descript" type="text/plain" style="height: 150px; width: 98%; margin-left: 10px;">
						
				</script>
			</div>
		</div>
		<div class="layui-form-item">
    		<label class="layui-form-label">排序：</label>
    		<div class="layui-input-block">
      			<input type="text" id="cat_order" name="cat_order" maxlength="30" value=""   lay-verify="cat_order" autocomplete="off" class="layui-input" style="display:inline;">&nbsp;
    		</div>
  		</div>
  		<div style="width:100%;height:40px;overflow:hidden;clear:both;"></div>
  		<div class="layui-layer-btn layui-layer-btn-" style="padding:8px 12px;z-index:1000;position: fixed;left: 1px;width: 98%;height: 40px; background:rgba(230,230,230,0.8);bottom:0px;"> 
  			[ <a id="remove" href="#" title="删除节点" onclick="return false;">删除节点</a> ]
  			[ <a id="addLeaf" href="#" title="增加叶子节点" onclick="return false;">增加叶子节点</a> ]
  			<a class="layui-layer-btn0" type="button" id="savebtn"  lay-submit="" lay-filter="edistore">保存</a>
  			<a class="layui-layer-btn1" type="button" id="restbtn"  layposition: fixed;left: 1px;width: 98%;height: 40px; background:rgba(230,230,230,0.8)-submit="" lay-filter="">取消</a>
  		</div>
  	</form>
	</div>
	
	
</div>





<script type="text/javascript">	
var ue = UE.getEditor('editor1', {
	zIndex : 0,
});



var index = parent.layer.getFrameIndex(window.name);

layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form()
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate;
    
    
    form.verify({
    	name:function(value){
    		if($.trim(value) == ""){
    			return "请输入名称";
    		}
    	},
    	path:function(value){
    		if($.trim(value) == ""){
    			return "请输入文件夹";
    		}
    	},
    });
    //取消按钮
	$(".layui-layer-btn1").click(function(){
    	parent.layer.close(index);
    }); 
    
form.on('submit(edistore)', function(data){
	var url=ctx+"/cms/admin/cat/save-add.do";
	var message="保存成功";
	$.ajax({
		url : ctx+"/cms/admin/cat/check-repeat.do?name="+$("#name").val(),
		dataType : "json",
		async:false,
		success : function(res) {
			if(res > 0){
				url=ctx+"/cms/admin/cat/save-edit.do";
				message="修改成功";
			}
		}
	});
      var options = {
     			url:url,
     			type:"post",
     			data: {"parent_id":parent_id},
     			dataType:"json",
     			success:function(data){
     				if(data.result==1){	
     					parent.$.Loading.success(message);
     					isSave = 1;   
     				}else{
     					parent.$.Loading.error(data.message);
     					isSave = 0;  
     				}
     			}
     		};
     		$("#editForm").ajaxSubmit(options); 
    });

});
</script>





<#include '/admin/footer.html' >